package com.adinnet.admin.peaseRecord.service.impl;

import com.adinnet.admin.classs.model.vo.ClassResp;
import com.adinnet.admin.order.model.vo.OrderResp;
import com.adinnet.admin.peaseRecord.model.PeaseRecord;
import com.adinnet.admin.peaseRecord.mapper.PeaseRecordMapper;
import com.adinnet.admin.peaseRecord.model.vo.PeaseRecordReq;
import com.adinnet.admin.peaseRecord.model.vo.PeaseRecordResp;
import com.adinnet.admin.peaseRecord.service.PeaseRecordService;
import com.adinnet.admin.school.mapper.SchoolMapper;
import com.adinnet.admin.school.model.School;
import com.adinnet.admin.system.model.vo.SysUserInfo;
import com.adinnet.common.biz.enums.RoleCode;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 豌豆记录表 服务实现类
 * </p>
 *
 * @author zYshuai
 * @since 2020-12-15
 */
@Service
public class PeaseRecordServiceImpl extends ServiceImpl<PeaseRecordMapper, PeaseRecord> implements PeaseRecordService {


    @Autowired
    private PeaseRecordMapper peaseRecordMapper;

    @Autowired
    private SchoolMapper schoolMapper;


    /**
     * 超级管理员查询所有金豆明细
     * 机构查询所管辖校区下的
     * 学校查询本校
     * 老师查询本校
     *
     * @param peaseRecordReq:
     * @param sysUserInfo:
     * @author zYshuai
     * @Date: 2020/12/15 17:13
     * @return: java.util.List<com.adinnet.admin.peaseRecord.model.vo.PeaseRecordResp>
     */
    @Override
    public PageResult<PeaseRecordResp> peaseRecordList(PeaseRecordReq peaseRecordReq, SysUserInfo sysUserInfo) {
        if ((Long) peaseRecordReq.getPage() == null || (Long) peaseRecordReq.getLimit() == null) {
            peaseRecordReq.setPage(1);
            peaseRecordReq.setLimit(10);
        }
        Page page = new Page<OrderResp>(peaseRecordReq.getPage(), peaseRecordReq.getLimit());
        List<PeaseRecordResp> recordResps = Lists.newArrayList();
        if (RoleCode.ADMIN.getCode().equals(sysUserInfo.getMaxRole())) {
            recordResps = peaseRecordMapper.peaseRecordList(page, peaseRecordReq.getClassId(), peaseRecordReq.getSchoolId(), peaseRecordReq.getOrganizationId(), peaseRecordReq.getKeyWords(), null);
        }
        if (RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())) {
            peaseRecordReq.setOrganizationId(sysUserInfo.getOrganizationId());
            recordResps = peaseRecordMapper.peaseRecordList(page, peaseRecordReq.getClassId(), peaseRecordReq.getSchoolId(), peaseRecordReq.getOrganizationId(), peaseRecordReq.getKeyWords(), null);
//            if (RoleCode.ORGANIZATION.getCode().equals(sysUserInfo.getMaxRole())) {
//
//                if (CollectionUtils.isNotEmpty(recordResps)) {
//                    List<PeaseRecordResp> peaseRecordResps = Lists.newArrayList();
//                    QueryWrapper<School> queryWrapper = new QueryWrapper();
//                    //查询机构管辖的校区
//                    queryWrapper.eq("organization_id", sysUserInfo.getOrganizationId()).eq("deleted", 0);
//                    List<School> schools = schoolMapper.selectList(queryWrapper);
//                    if (CollectionUtils.isNotEmpty(schools)) {
//                        for (PeaseRecordResp recordResp : recordResps) {
//                            for (School school : schools) {
//                                if (school.getId() == recordResp.getSchoolId()) {
//                                    peaseRecordResps.add(recordResp);
//                                }
//                            }
//                        }
//                        return peaseRecordResps;
//                    }
//                    return recordResps;
//                }
//            }
        }
        if (RoleCode.SCHOOL.getCode().equals(sysUserInfo.getMaxRole())) {
            recordResps = peaseRecordMapper.peaseRecordList(page, peaseRecordReq.getClassId(), peaseRecordReq.getSchoolId(), peaseRecordReq.getOrganizationId(), peaseRecordReq.getKeyWords(), sysUserInfo.getSchoolId());
        }
        if (RoleCode.CLASSS.getCode().equals(sysUserInfo.getMaxRole())) {
            recordResps = peaseRecordMapper.peaseRecordList(page, peaseRecordReq.getClassId(), peaseRecordReq.getSchoolId(), peaseRecordReq.getOrganizationId(), peaseRecordReq.getKeyWords(), sysUserInfo.getSchoolId());
        }

        return new PageResult<>(recordResps, page.getTotal());
    }
}
